const { expect } = require("chai")
const { ethers } = require("hardhat")

describe("DNFBattle 测试", function () {
    let GameToken
    let gameToken
    let DNFBattle
    let dnfBattle
    let owner
    let player1

    beforeEach(async function () {
        // 获取测试账户
        ;[owner, player1] = await ethers.getSigners()

        // 部署DFT代币合约（注意：新版本ethers.js不需要deployed()）
        GameToken = await ethers.getContractFactory("GameToken")
        gameToken = await GameToken.deploy() // 直接部署，返回合约实例
        // 部署游戏合约
        DNFBattle = await ethers.getContractFactory("DNFBattle")
        dnfBattle = await DNFBattle.deploy(gameToken.target, owner.address) // 直接部署
        // 向游戏合约充值DFT代币
        const rewardAmount = ethers.parseEther("10000")
        await gameToken.transfer(dnfBattle.target, rewardAmount)
        //给玩家1转一些代币
        await gameToken.transfer(player1.address, ethers.parseEther("1000"))
    })
    it("get gameToken balance", async function () {
        // 测试游戏合约的DFT余额
        const balance = await dftToken.balanceOf(dnfBattle.target)
        expect(balance).to.equal(ethers.parseEther("10000"))
    })
    it("get gameToken balance", async function () {
        // 测试游戏合约的DFT余额
        const balance = await dftToken.balanceOf(dnfBattle.target)
        expect(balance).to.equal(ethers.parseEther("10000"))
    })
    it("create hearo and fight", async function () {
        await dnfBattle.connect(player1).createHero("Warrior", {
            value: ethers.parseEther("0.005"),
        })
        // 检查玩家1的英雄
        const heroId = await dnfBattle.playerHero(player1.address)
        expect(heroId).to.be.gt(0)
        //玩家1挑战怪物0，奖励dft
        await expect(dnfBattle.connect(player1).fightMonster(0))
            .to.emit(dnfBattle, "BattleFought")
            .withArgs(
                player1.address,
                heroId,
                0,
                true,
                ethers.parseEther("100")
            )
        //检查1的奖励
        const player1DFTbalance = await gameToken.balanceOf(player1.address)
        expect(player1DFTbalance).to.equal(ethers.parseEther("1100"))
    })
    it("奖池gameToken 不足时应该失败", async function () {
        // 1. 先给owner授权从合约中提取代币的权限
        const allAmount = await gameToken.balanceOf(dnfBattle.target)
        // 关键修复：通过游戏合约授权owner提取代币（代币持有者是合约）
        await dnfBattle.connect(owner).approveWithdraw(owner.address, allAmount)
        // 耗尽奖励池
        await gameToken
            .connect(owner)
            .transferFrom(dnfBattle.target, owner.address, allAmount)

        // 验证奖池已空
        const poolBalance = await gameToken.balanceOf(dnfBattle.target)
        expect(poolBalance).to.equal(0)
        // //创建hero
        await dnfBattle.connect(player1).createHero("Mage", {
            value: ethers.parseEther("0.005"),
        })
        //战斗，gametoken不足
        await expect(
            dnfBattle.connect(player1).fightMonster(0)
        ).to.be.revertedWith(/Insufficient reward in pool/)
    })
    it("getheroInfo,获取英雄信息", async function () {
        await dnfBattle.connect(player1).createHero("Mage", {
            value: ethers.parseEther("0.005"),
        })
        const [name, power, beforeWins] = await dnfBattle
            .connect(player1)
            .getHeroInfo()

        console.log("战斗前 - 胜率:", beforeWins.toString())
        // 执行战斗并捕获事件（关键调试）
        const fightTx = await dnfBattle.connect(player1).fightMonster(0)
        const fightReceipt = await fightTx.wait()
        // 战斗后英雄信息
        const [afterName, afterPower, afterWins] = await dnfBattle
            .connect(player1)
            .getHeroInfo()
        console.log("战斗后 - 胜率:", afterWins.toString())

        expect(afterWins).to.equal(1)
    })
})
